<?php

namespace App\Http\Controllers\Api;


use App\Models\ClassCard;
use App\Models\SportsTest;
use App\Models\Team;
use App\Models\User;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class UserController extends ApiController
{

    private $uid = '';


    public function __construct(Request $request)
    {
        $this->uid = auth('api')->id();
    }


    public function getUserList(Request $request){
          $page = isset($request['page']) ? $request['page'] : 1;
          $pageSize = isset($request['pageSize']) ? $request['pageSize'] : 15;
          $tea = new User();
          $data = $tea->getMemberList($page,$pageSize) ?? '';
      return self::returnAjax(['list'=>$data], 200, "学员风采");
    }

    //修改
    public  function editUser(Request $request){
        $id = $this->uid;
        $user_nickname = $request['user_nickname'];
        $pic = $request['user_img'];

        //修改用户信息
        $res = User::updateUser($id,$user_nickname,$pic);
//        if(!$res){
//            return parent::ajaxReturn(201, new \stdClass(), '修改失败');
//        }
        return self::returnAjax('', 200, "修改成功");
    }
    //家长绑定用户信息
     public function bindUser(Request $request){
           $user_tel = $request['user_tel'];
           $code = $request['code'];
           if (!SmsService::verifySmsCode($user_tel,$code)){
               return parent::ajaxReturn(1000, new \stdClass(), '验证码无效');
           }
           //家长id
          $tid = $this->uid;
//          $tid = 3;
          DB::beginTransaction();
         try {
             User::where('user_phone',$user_tel)->update(['parent_id'=>$tid]);
             User::where('id',$tid)->update(['level'=>2]);
             DB::commit();
             return self::returnAjax('', 200, "修改成功");
         }catch (Exception $e){
             DB::rollBack();
             return self::returnAjax('', 201, "修改失败");
         }
     }
     //申请报名
    public function apply(Request $request){
        if (Cache::has('get_luck_lock' . $this->uid)) {
            return parent::ajaxReturn(201, new \stdClass(), '操作频繁');
        }
        Cache::put('get_luck_lock' . $this->uid, '1', 0.1);

        //课时Id
        $cardId = $request['card_id'];
        $uid = $this->uid;
        if ($this->isUp($uid,$cardId)){
            return parent::ajaxReturn(201, new \stdClass(), '您已经报名过');
        }
        $userInfo = User::where('id',$uid)->first();
        $res = DB::table('class_card')->insert(
              ['user_id'=>$uid,
               'user_tel'=>$userInfo->user_phone,
               'user_name'=>$userInfo->user_nickname,
               'card_id'=>$cardId,
               'created_at'=>time(),
               'type'=>1,
        ]);
        if(!$res){
            return parent::ajaxReturn(201, new \stdClass(), '报名失败');
        }
        return self::returnAjax('', 200, "报名成功");
    }

    protected function isUp($uid,$cardId){
       $classInfo = ClassCard::where('user_id',$uid)->where('card_id',$cardId)->select()->get()->toArray();
       $len = count($classInfo);
       //查询当前的申请时间
        if ($len == 0){
            if ($classInfo){
                return true;
            }
        }
        if ($len>0){
           $class_end_time = User::where('id',$uid)->value('class_end_time');
           if (time()<$class_end_time){
               return true;
           }
       }
       return false;
    }


    public function getDetailUser(Request $request){
       $uid =$request['id'];
       $res = User::getDetailUser($uid);
       return self::returnAjax($res, 200, "success");
    }

    public function getMyOrder(){
        $uid = $this->uid;
//        $uid = 1;
        $res = ClassCard::getMyOrder($uid);
        return self::returnAjax($res, 200, "success");

    }
    //学员用户信息接口
    public function userInfo(){
        $uid = $this->uid;
        $user = User::where('id',$uid)->first(['team_id','period','level','class_end_time','user_headimgurl']);
        //查询用户体测条数
        $num = SportsTest::where('user_id',$uid)->count();
        if ($user->level != 1){
            return self::returnAjax('', 201, "此人不是学员");
        }
         $data=[];
         //查找当前人剩余课时
        if ($user->team_id != 0){
            $team = Team::overTime($user->team_id);
            $data['team_name'] = $team->team_name;
            $data['team_status'] = $team->team_status;
            $data['tea_log'] = $team->tea_log;
            $data['team_id'] = $user->team_id;
        }
        $timediff = $user->class_end_time-time();
        $days = intval($timediff/86400);
        if ($days<=0){
            $days = 0;
        }
        //查找
        if ($user->period>=0){
            $data['period'] = $user->period;
        }else{
            $data['period'] = 0;
        }
        $data['end_sign'] = $days;
        $data['level'] = $user->level;
        $data['user_headimgurl'] = $user->user_headimgurl;
        $data['user_id'] = $this->uid;
        $data['sport_num'] = $num;
        return self::returnAjax($data, 200, "success");
    }


    public function getLevel(){
        $uid = $this->uid;
        $level = User::where('id',$uid)->value('level');
        $data['level'] = $level;
        return self::returnAjax($data, 200, "success");
    }
}
